﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcEngine.Mvc.Controllers;
using MvcEngine.Core;
using MvcEngine.Core.Helpers;
using MvcEngine.Core.Validation;
using MvcEngine.Mvc.Models;
using System.Web.Mvc;
using MvcEngine.Core.Services;
using MvcEngine.Core.Repositories;

namespace MvcEngine.CustomizationTest
{
    public class AccountControllerCustomization : AccountController
    {
        [AcceptVerbs(HttpVerbs.Post)]
        [ActionName("Register")]
        public override object RegisterAccount(FormCollection form)
        {
            AccountViewModelCustomization model = new AccountViewModelCustomization();
            UpdateModel<AccountViewModel>(model, form.ToValueProvider());

            if (ValidatePasswords(model.Password, model.ConfirmPassword))
            {
                try
                {
                    IUserTest user = IoC.Resolve<IUserTest>();
                    user.Name = model.Email;
                    user.Email = model.Email;
                    user.Password = model.Password;
                    user.Birthdate = model.Birthdate;
                    user.IsApproved = true;

                    IUserRepository userRepository = IoC.Resolve<IUserRepository>();
                    userRepository.Add(user);

                    FormsAuthenticationHelper.Current.SignIn(model.Email, false);
                    //EmailSender.Current.SendNotificationMessage
                    //    (
                    //        //user, 
                    //        //NavigateHelper.Current.RegisterSuccess(this)
                    //    );
                }
                catch (ValidationException e)
                {
                    ValidationHelper.Current.UpdateModelState(e, ModelState);
                    return Register();
                }
                //return Redirect(NavigateHelper.Current.Home(this));
            }
            return Register();
        }
    }
}
